package cate.game.res.guild;

import cate.game.GameBody;
import cate.game.role.res.guild.po.GuildPO;
import cate.game.role.res.guild.po.basic.GuildBasicPO;
import cp.solution.business.game.guild.GuildModifyCmd;
import cp.solution.business.game.guild.GuildSnapshot;
import cp.solution.business.game.guild.GuildSnapshotList;
import easy.java.practice.errorcode.EcException;
import easy.java.time.DateTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@Slf4j
public class GuildForGm {

    private static GuildSnapshot toSnapshot(GuildBasicPO po) {
        GuildSnapshot s = new GuildSnapshot();
        s.uid = po.uid;
        s.guildNo = po.guildNo;
        s.level = po.level;
        s.exp = po.exp;
        s.power = po.power;
        s.guildName = po.guildName;
        s.presidentName = po.presidentName;
        s.presidentId = po.presidentId;
        s.memberNum = po.memberNum;
        s.memberLimit = po.memberLimit;
        s.authentication = po.authentication;
        s.levelRequired = po.levelRequired;
        s.declaration = po.declaration;
        s.createTime = po.createTime;
        s.editableTime = po.editableTime;
        s.activeWeek = po.activeWeek;
        return s;
    }

    public static GuildSnapshotList getList(GameBody game) {
        GuildSnapshotList g = new GuildSnapshotList();
        g.srvId = game.config.srvId;
        g.srvName = game.config.gs.getName();
        List<GuildPO> guildList = game.res.guild.getGuildList();
        if(guildList != null) {
            for (GuildPO guild : guildList) {
                g.list.add(toSnapshot(guild.basic));
            }
        }
        return g;
    }

    public static String modify(GameBody game, GuildModifyCmd cmd) {
        GuildPO guild = game.res.guild.getGuild(cmd.getGuildUid());
        if(guild == null) {
            return "找不到uid="+cmd.getGuildUid()+"的协会";
        }
        log.info("将对协会 {} ({}) 执行GM协会修改命令 {}", guild.basic.guildName, guild.uid, cmd);
        if(StringUtils.isBlank(cmd.getType())) {
            throw new EcException("type不能为空").s();
        }
        switch (cmd.getType()) {
            case GuildModifyCmd.TYPE_NAME:
                return modifyName(guild, cmd);
            case GuildModifyCmd.TYPE_DECLARATION:
                return modifyDeclaration(guild, cmd);
            case GuildModifyCmd.TYPE_EDITABLE_TIME:
                return modifyEditableTime(guild, cmd);
        }
        throw new EcException("不支持的修改 by " + game + " cmd=" +cmd).s();
    }

    private static String modifyName(GuildPO guild, GuildModifyCmd cmd) {
        guild.basic.guildName = cmd.getValueString();
        guild.basic.noticeUpdateAll();
        return "协会名称修改成功";
    }

    private static String modifyDeclaration(GuildPO guild, GuildModifyCmd cmd) {
        guild.basic.declaration = cmd.getValueString();
        guild.basic.noticeUpdateAll();
        return "协会宣言修改成功";
    }

    private static String modifyEditableTime(GuildPO guild, GuildModifyCmd cmd) {
        guild.basic.editableTime = cmd.getValueLong();
        return "协会允许编辑时间修改为" + DateTool.instance().format(cmd.getValueLong());
    }
}
